

package com.sunny.gateway;

import com.sunny.kratos.common.delay.DelayTask;
import com.sunny.kratos.common.delay.DelayTaskService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.UUID;

/**
 * 网关应用
 *
 * @author sunny
 * @date 2025/05/30
 */
@EnableDiscoveryClient
@SpringBootApplication

public class SunnyGatewayApplication implements CommandLineRunner {

	@Autowired
	DelayTaskService queueTaskService;

	public static void main(String[] args) {
		SpringApplication.run(SunnyGatewayApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		DelayTask<String> queueTask = new DelayTask();
		queueTask.setTaskId(UUID.randomUUID().toString());
		queueTask.setTask("启动任务");
		queueTask.setTopic("task13");
		queueTask.setStartTime(LocalDateTime.now().plus(120, ChronoUnit.SECONDS)); // 5分钟后执行

		queueTaskService.addTask(queueTask);

		Thread.sleep(2000);
		queueTaskService.removeTask(queueTask);
		DelayTask task = new DelayTask();
		task.setTaskId(UUID.randomUUID().toString());
		task.setTask("启动任务1");
		task.setTopic("task14");
		task.setTraceId(UUID.randomUUID()+"");
		task.setStartTime(LocalDateTime.now().plus(1, ChronoUnit.MINUTES)); // 5分钟后执行
		queueTaskService.addTask(task);

// 传统方式
		DelayTask task1 = new DelayTask();
		task1.setTaskId(UUID.randomUUID().toString());
		task1.setTask("启动任务2");
		task1.setTopic("task14");
		task1.setStartTime(LocalDateTime.now().plus(1, ChronoUnit.MINUTES)); // 5分钟后执行
		queueTaskService.addTask(task1);
		// 这里可以添加一些启动时需要执行的逻辑
		// 比如加载配置、初始化数据等
		System.out.println("Sunny Gateway Application has started successfully!");
		// 可以在这里添加其他初始化代码
		// 例如加载路由配置、初始化缓存等
	}
}
